#include "drv_rtc.h"

#define BKP_REG_DATA	0x5050


uint8_t rtc_init(void)
{
	u8 res=0;
	/* enable pwc and bpr clocks */
	crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);

	/* enable the battery-powered domain write operations */
	pwc_battery_powered_domain_access(TRUE);

	/* check if rtc is initialized */
	if(bpr_data_read(BPR_DATA1) != BKP_REG_DATA)
	{
		/* reset battery-powered domain register */
		bpr_reset();

		/* enable the lext osc */
		crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
		/* wait lext is ready */
		while(crm_flag_get(CRM_LICK_STABLE_FLAG) == RESET);
		/* select the rtc clock source */
		crm_rtc_clock_select(CRM_RTC_CLOCK_LICK);

		/* enable rtc clock */
		crm_rtc_clock_enable(TRUE);

		/* wait for rtc registers update */
		rtc_wait_update_finish();

		/* wait for the register write to complete */
		rtc_wait_config_finish();

		/* set rtc divider: set rtc period to 1sec */
		rtc_divider_set(40000-1);

		/* wait for the register write to complete */
		rtc_wait_config_finish();

		/* set time */
		//    rtc_time_set(calendar);
		set_date(2025,11,01);
		set_time(0, 0, 0);
		/* writes data to bpr register */
		bpr_data_write(BPR_DATA1, BKP_REG_DATA);

		printf("RTC reset timestamp.\r\n");
		res = 1;
	}
	else
	{
		/* wait for rtc registers update */
		rtc_wait_update_finish();

		/* wait for the register write to complete */
		rtc_wait_config_finish();

		res = 0;
	}

	printf("System time:");
	date();


	return res;
}

#include "led.h"
/**
  * @brief  this function handles rtc handler.
  * @param  none
  * @retval none
  */
void RTC_IRQHandler(void)
{
	if(rtc_flag_get(RTC_TA_FLAG) != RESET)
	{
		/* clear the rtc second flag */
//		led_state_set(LED1, LED_TOGGLE);
		rtc_flag_clear(RTC_TA_FLAG);
	}
}


void set_date(uint32_t year, uint32_t month, uint32_t day)
{
	time_t now;
	struct tm *p_tm;
	struct tm tm_new;

	/* get current time */
	now = time(NULL);
	/* converts calendar time time into local time. */
	p_tm = localtime(&now);
	/* copy the statically located variable */
	memcpy(&tm_new, p_tm, sizeof(struct tm));

	/* update date. */
	tm_new.tm_year = year - 1900;
	tm_new.tm_mon  = month - 1; /* tm_mon: 0~11 */
	tm_new.tm_mday = day;

	/* converts the local time in time to calendar time. */
	now = mktime(&tm_new);

	set_rtc_time_stamp(now);
}

/**
 * Set system time(date not modify).
 *
 * @param uint32_t hour   e.g: 0~23.
 * @param uint32_t minute e.g: 0~59.
 * @param uint32_t second e.g: 0~59.
 *
 * @return null.
 *
 */
void set_time(uint32_t hour, uint32_t minute, uint32_t second)
{
	time_t now;
	struct tm *p_tm;
	struct tm tm_new;

	/* get current time */
	now = time(NULL);

	/* converts calendar time time into local time. */
	p_tm = localtime(&now);
	/* copy the statically located variable */
	memcpy(&tm_new, p_tm, sizeof(struct tm));

	/* update time. */
	tm_new.tm_hour = hour;
	tm_new.tm_min  = minute;
	tm_new.tm_sec  = second;

	/* converts the local time in time to calendar time. */
	now = mktime(&tm_new);

	set_rtc_time_stamp(now);
}

static void set_rtc_time_stamp(time_t time_stamp)
{
	/* enable pwc and bpr clocks */
	crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
	/* enable write access to bpr domain */
	pwc_battery_powered_domain_access(TRUE);

	/* set the rtc counter value */
	rtc_counter_set(time_stamp);

	/* wait for the register write to complete */
	rtc_wait_config_finish();
}

time_t get_rtc_timestamp(void)
{
	return ((time_t)rtc_counter_get());;
}


void date(void)
{
	char buff[30]= {0};
	time_t now;
	struct tm *p_tm;

	/* get current time */
	now = time(NULL);

	/* converts calendar time time into local time. */
	p_tm = localtime(&now);
	strftime(buff, 30, "%Y-%m-%d %H:%M:%S", p_tm);
	printf("\r\n%s\r\n", buff);
}

void rtc_modfiy_date(uint32_t year, uint32_t month, uint32_t day, uint32_t hour, uint32_t minute, uint32_t second)
{
	time_t now;

	set_date(year, month, day);
	set_time(hour, minute, second);

	time(&now);
	printf("\r\n%s\r\n", ctime(&now));
}


//#include <stdlib.h>
//#include <rtthread.h>
//void set_sys_date(int argc, char**argv)
//{ 
//	if (argc < 7)
//    {
//        printf("Please input: set_sys_date 2022 9 1 12 59 59");
//        return;
//    }
//	
//	rtc_modfiy_date(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), atoi(argv[6]));
//}
//MSH_CMD_EXPORT(set_sys_date, set system date);

